﻿#pragma once

#include "qtypes.h"

#include <WinBase.h>




namespace micro{
namespace core{


// A baseclass for synchronisation classes.
class QSyncObject
{

protected:
	// Data.
	HANDLE	m_hHandle;


public:
	// Contruction.
	QSyncObject() : m_hHandle( nullptr ) {;}

	// Destruction.
	virtual ~QSyncObject()
	{
		 Close();
	}

	void Close()
	{
		BOOL rc = FALSE;
		if ( m_hHandle )
		{
			rc = ::CloseHandle( m_hHandle );
		}

		m_hHandle = nullptr;
	}

	// Pure virtuals. Must be overidden in derived
	// classes.
	virtual bool Lock() = 0;
	virtual bool Unlock() = 0;

	// Overide when necessary.
	virtual bool Lock( DWORD dwTimeout = INFINITE )
	{
		// Lock the handle.
		ASSERT( m_hHandle );
		if ( ::WaitForSingleObject( m_hHandle, dwTimeout ) == WAIT_OBJECT_0 )
			return true;
		return false;
	}

	// Operator overloads.
	inline operator HANDLE() const	{ return m_hHandle; }
	inline operator HANDLE()	    { return m_hHandle; }

};


 

}
}//micro